const EXPayUtil = require('EXPayUtil');
/**
 * 商户号
 */
let merchantNo= "2019070416280640682";
/**
 * 应用号
 */
let applicationNo= "2019070418053528355";
/**
 * 支付通道
 */
let channel= "wx_lite";

/**
 * 参与加密的字符串
 */
let encryptKey ="ExpectX";


/**
 * 参与签名的密钥
 */
let signKey ="XZcHMCChilaEyusCnTAgZYyvjwJYfRjWvpEdQjQvIkAMzssFztec6f6a1e946d43e1984772fdd3883434";

/**
 * 支付网关
 */
let gateUrl = "http://titifootball.com:1111/pay/wxpay";

 
//根据判断message 是否显示loading
function postRequestLoading(url, params, message, success, fail) {
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  const postRequestTask = wx.request({
    url: url,
    data: params,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    success: function (res) {
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200) {
        success(res)
      } else {
        wx.showToast({
          title: res.data.message || '',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        fail(res)
      }
    },
    fail: function (res) {
      if (message != "") {
        wx.hideLoading()
      }
      fail(res)
    }
  })
}
 
 
/**
 * 发起支付
 */
function doEXPay(params,success, fail, complete){ 
  var payParams = {
    merchantNo: merchantNo,
    applicationNo: applicationNo,
    channel: channel,
    merchantOrderId:params.merchantOrderId,
    orderName:params.orderName,
    orderDesc:params.orderDesc,
    orderDate:params.orderDate,
    tradeAmount:params.tradeAmount,
    openId:params.openId
  };
  payParams.sign = EXPayUtil.generateSignature(payParams, signKey);
  payParams.Encrypt = EXPayUtil.setEncrypt(payParams, encryptKey);

  postRequestLoading(gateUrl, payParams, "支付中...",
    function (res) {
      if (res.statusCode == 200) {
        if (res.data.code == 200) {
          console.log(res.data.data.timeStamp, res.data.data.nonceStr, res.data.data.package, res.data.data.paySign)
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp + '',
            'nonceStr': res.data.data.nonceStr + '',
            'package': res.data.data.package + '',
            'signType': 'MD5',
            'paySign': res.data.data.paySign + '',
            'success': function (res) {
              wx.hideLoading()
              success(res);
            },
            'fail': function (res) {
              wx.hideLoading();
              fail(res);

            },
            'complete': function (res) {
              wx.hideLoading();
              complete(res);
            }
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 2000
        })
      }

    }, function (res) {
    });
}

module.exports = {
  
  doEXPay: doEXPay
}